<?php 
namespace News\Model;
use Think\Model;

class NewsReplyModel extends Model {
	
	
	/**
	* 图恩回复内容编辑啊
	*
	*/
	public function FormSubmit($rid = 0) {
		
		//处理添加
		$news_title_new = I('title');
		$rule_id        = I('rule_id');
		$newspicture	= I('newspicture','','trim');
		$newsurl        = I('newsurl','','trim');
		$newstitle      = I('newstitle','','trim');
		
		if(!empty($newsurl) && strpos($newsurl,'http') === false){
			$newsurl = 'http://'.$newsurl; 
		}
		
		if (empty($rule_id)) {
			//插入
			$parentid = 0;
			//I('is_picture',0,'trim')
			$insert = array(
				'rule_id' => $rid,
				'parentid' => $parentid,
				'title' => $newstitle,
				'description' => I('newsdescription','','trim'),
				'thumb' => $newspicture,
				'content' => I('newscontent','','trim'),
				'url' => $newsurl,
				'is_picture'=>1
			);
			
			$pid = M('NewsReply')->add($insert);
			if(empty($parentid)){
				$parentid = $pid;
			}
		}else{
			
			//更新
			$data = array(
				'title' => $newstitle,
				'description' => I('newsdescription','','trim'),
				'thumb' => $newspicture,
				'content' => I('newscontent','','trim'),
				'url' => $newsurl,
				'is_picture'=>1
			);
			
			$pid = M('NewsReply')->where('rule_id = %d',$rule_id)->save($data);
			if(empty($parentid)){
				$parentid = $pid;
			}
		
		
		}
		
		//删除图文
		$delid = I('delnews');
		if(!empty($delid)){
			$idarray=explode(',',$delid);
			foreach($idarray as $id){
				if(!empty($id)){
					$newrep = M('NewsReply')->where("rule_id='{$rid}' and id='{$id}'")->field("id,parentid")->find();

					M('NewsReply')->where(array('id'=>$id))->delete();

					if(empty($newrep['parentid'])){
						M('NewsReply')->where(array('parentid'=>$id))->delete();
					}
				}
			}
		}
	}

}
